﻿using System;
using System.Collections.Generic;
using NHibernate.Criterion;
using Ornament.NHibernates;
using Products.Databases;

namespace Products.DataAcccess
{
    public class VersionDao : DaoBase<Guid, ProductVersion>
    {
        protected override Order DefaultOrder
        {
            get { return Order.Desc("OrderValue"); }
        }

        public IList<ProductVersion> GetNewChangeVersion(Product product, DatabaseObject databaseObject)
        {
            DetachedCriteria main = DetachedCriteria.For(typeof(ProductVersion));
            DetachedCriteria a = DetachedCriteria.For(databaseObject.GetType())
                .SetProjection(Projections.Property("ve.Version"))
                .Add(Restrictions.Eq("Id", databaseObject.Id))
                .CreateCriteria("Snapshots", "ve")
                .CreateCriteria("Version");

            main.Add(Subqueries.PropertyNotIn("Id", a));


            DetachedCriteria b = DetachedCriteria.For(typeof(Product))
                .Add(Restrictions.Eq("Id", product.Id))
                .CreateCriteria("Versions", "ds")
                .SetProjection(Projections.Property("ds.Id"));

            main.Add(Subqueries.PropertyIn("Id", b));

            return main.GetExecutableCriteria(CurrentSession).List<ProductVersion>();
        }
    }
}